package com.guchenbo.spark.sql

import com.guchenbo.spark.sql.CsvReader.path

object ChineseTable {
  def main(args: Array[String]): Unit = {
    val spark = SparkUtils.sparkSession("demo")

    var table = "turing.ch_01"
    var sql = s"DROP TABLE IF EXISTS $table"
    spark.sql(sql)

    sql = s"CREATE TABLE IF NOT EXISTS $table (`姓名` STRING, `年龄` INTEGER, `工作` STRING, `生日` STRING) STORED AS PARQUET"
    spark.sql(sql)

    val sr = spark.read
      .option("sep", ";")
      .option("encoding", "UTF-8")
      .option("header", "true")

    val filepath = SparkUtils.classpath("people.csv")

    val df = sr.csv(filepath)

    var tableTmp = s"${table}_tmp"
    df.createOrReplaceTempView(tableTmp)
    sql = s"INSERT OVERWRITE TABLE $table SELECT * FROM $tableTmp"
    spark.sql(sql)

    spark.sql(s"SELECT * FROM $table").show()

    sql = s"LOAD DATA LOCAL INPATH '$filepath' OVERWRITE INTO TABLE $table"
    spark.sql(s"SELECT * FROM $table").show()

  }
}
